<?php
/**
 * ajax.php
 */

require_once "./include/common.inc.php";

$data = '';
switch ($action)
{
	case'cachemod':
	break;
	case 'getcontentcomment':
		if(empty($contentid)) return false;
		$c = load("comment.class.php");
		$comment = $c->show($contentid, 'content', "{$siteurl}ajax.php?action=getcontentcomment&contentid=$contentid", $page, 5);
		$pageurl = $c->mutipage;
		$pageurl = str_replace('pages', 'commentpages', $pageurl);
		$html = '<div style="clear:both;"></div>';
		if($comment)
		{
		foreach ($comment as $val)
		{
			$val['username'] = $val['username'] ? '<a href="http://home.52holiday.com/space.php?uid='.$val['uid'].'" target="_blank"><strong>'.$val['username'].'</strong></a>' : '假日网友';
			$html .= '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
			$html .= '<tr><td class="username">'.$val['username'].' 发表于：'.s_date($val['pubtime']).'</td></tr><tr><td class="content">'.$val['comment'].'</td></tr>';
			$html .= '</table>';
		}
		$html .= '<center>'.$pageurl.'</center>';
		$html .= "<script type=\"text/javascript\">$('.commentpages a').ajaxContent({ target:'#contentcomment' });</script>";
		}
		else
		{
			$html = '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td><center>暂无评论,就等您了</center></td></tr></table>';
		}
		$data = $html;
		break;
		
	case 'putcontentcomment':
		if(empty($contentid) || empty($content)) return false;
		periodscheck();
		//cookie限制评论太快
		$commentcookie = $_COOKIE[$cookiepre.'contentcomments'];
		!strstr($commentcookie, "{$contentid},") ? @s_setcookie('contentcomments', $commentcookie."{$contentid},", 180) : exit('1');
			
		$vistername = HConvert(SafeConvert($vistername));
		$tel = HConvert(SafeConvert($tel));
		$mail = HConvert(SafeConvert($mail));
		$content = HConvert(SafeConvert($content));
		$vistername = censor($vistername);
		$tel = censor($tel);
		$mail = censor($mail);
		$content = censor($content);
		$db->query("INSERT INTO {$tablepre}comment(contentid, uid, username, tel, mail, comment, ip, pubtime, status) VALUES('$contentid', '$uid', '$vistername', '$tel', '$mail', '$content', '$ip', '$timestamp', '$commentstatus')");
		//增加文章评论数
		$db->query("UPDATE {$tablepre}content SET comments=comments+1 WHERE contentid='$contentid'");
		
//		if($commentstatus) //1不审核状态下显示评论内容
//		{
//			$html = '<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background:#fbfada;">';
//			$html .= '<tr><td class="username">'.($username ? $username : '假日网友').' 发表于：'.s_date($timestamp).'</td></tr><tr><td class="content">'.$content.'</td></tr>';
//			$html .= '</table>';
//		}

//		$data = $commentstatus ? $html : '2';
		$data = '2';
		break;
	//ajax获取更多探店记列表
	case 'content':
		if(empty($sid) || empty($contentid)) return false;
		$sql = '';
		$page = max(intval($page), 1);
		$offset = 5*($page-1);
        $limit = " LIMIT $offset, 5";
		$sql = "SELECT title, url FROM `{$tablepre}content` WHERE shopid='$sid' AND status>0 AND contentid != '$contentid'";
		$num = $db->num_rows($db->query($sql));
		$sql .= " ORDER BY contentid DESC $limit";
		$contentlist = $db->fetch_all($sql);
		$contentpageurl = multi($num, 5, $page, "{$siteurl}ajax.php?action=content&sid=$sid&contentid=$contentid");

		$html = '';
		$html.='<div class="moreshop" id="contentother"><div class="top"><img src="http://static.52holiday.com/soudian/images/shop_66.png" /></div><div class="nr"><ul>';
		foreach($contentlist as $cl)
		{
			$html.='<li><a href="'.$siteurl.$cl[url].'" target="_blank" title="'.$cl[title].'">'.$cl[title].'</a></li>';
		}
		$html.='</ul></div>';
		$html.='<div class="lbfy">'.$contentpageurl.'</div></div>';
		$html.="<script type=\"text/javascript\">$('.pages a').ajaxContent({ target:'#contentother' });</script>";
		$data = $html;
		break;

	case 'updatecontentviews':
		if(empty($contentid)) return false;
		$contentviewscookie = $_COOKIE[$cookiepre.'contentviews'];
		!strstr($contentviewscookie, "{$contentid},") ? @s_setcookie('contentviews', $contentviewscookie."{$contentid},", 600) : exit;
		$db->query("UPDATE {$tablepre}content SET views=views+1 WHERE contentid='$contentid'");
		break;

	case 'login':
		include(SITE_ROOT.'./uc_client/client.php');
		include(SITE_ROOT."./uc_client/lib/db.class.php");
		$username = iconv("UTF-8","GBK",$username);

		list($uid, $username, $password, $email) = uc_user_login($username, $password);
		s_setcookie('cultureusers', '', -86400);
		if($uid > 0) {
			//用户登陆成功，设置 Cookie，加密直接用 uc_authcode 函数，用户使用自己的函数
			s_setcookie('cultureusers', uc_authcode($uid."\t".$username, 'ENCODE'), 31536000);
			//生成同步登录的代码
			//$ucsynlogin = uc_user_synlogin($uid);
			if($mod == 'index')
			{
				$data = "<div class=\"login_after\">
					<div><a href=\"http://bbs.52holiday.com/space-uid-{$uid}.html\" target=\"_blank\"><img src=\"http://ucenter.52holiday.com/avatar.php?uid={$uid}&size=small\" /></a></div>
					<div><a href=\"http://bbs.52holiday.com/space-uid-{$uid}.html\" target=\"_blank\" style=\"font-size:14px; font-weight:bold;\">{$username}</a> <span style=\"color:red; font-size:14px; font-weight:bold;\">欢迎您回来！</span></div>
					<div><a href=\"http://bbs.52holiday.com/pm.php\" target=\"_blank\">短消息</a> <a href=\"http://bbs.52holiday.com/my.php?item=threads\" target=\"_blank\" style=\"color:red;\">我的帖子</a> <a href=\"http://bbs.52holiday.com/memcp.php\" target=\"_blank\">个人中心</a></div>
					<div><a href=\"javascript:logout('index');\">退出登陆</a></div>
				</div>";
			}
			else
			{
				$data =  "欢迎您,<a href=\"http://home.52holiday.com/space.php?uid=".$uid."\" target=\"_blank\">".$username."</a> [<a href=\"javascript:;\" onclick=\"logout();\">退出</a>]";
			}
		}
		elseif($uid == -1) {
			$data = '-1';	//用户不存在,或者被删除
		}
		elseif($uid == -2) {
			$data = '-2';	//密码错误
		}
		break;
		
	case 'logout':
		s_setcookie('cultureusers', '', -86400);
			//生成同步退出的代码
			//$ucsynlogout = uc_user_synlogout();
		if($mod == 'index')
		{
			$data = '<div id="loginbox">
          					<form id="loginform" name="login" method="post" action="" onSubmit="return loginn(this);">
							<input name="mod" id="mod" type="hidden" value="index" />
            					<div class="login_lft">
              						<div>用户名 <input type="text" name="username" id="username" class="inpt" /></div>
              						<div>密&nbsp;&nbsp;码 <input type="password" name="password" id="password"  class="inpt"/></div>
            					</div>
            					<div class="login_rht"><input type="submit" name="button" id="button" value="&nbsp;" class="btn" /></div>
            					<div class="clear"></div>
          					</form>
          					<div class="buttonk"><a href="http://bbs.52holiday.com/logging.php?action=login" target="_blank"><img src="http://static.52holiday.com/bbs/images/password.jpg"/></a><a href="http://bbs.52holiday.com/register.php" target="_blank"><img src="http://static.52holiday.com/bbs/images/user.jpg"/></a><a href="http://bbs.52holiday.com/memcp.php" target="_blank"><img src="http://static.52holiday.com/bbs/images/center.jpg"/></a></div>
          					</div>';
		}
		else
		{
			$data = '<form id="loginform" name="login" method="post" action="" onsubmit="return loginn(this);">
                                    <input name="mod" id="mod" type="hidden" value="shop" />用户名：<input type="text" name="username" id="username" class="inptnm" />密码：<input type="password" name="password" id="password"  class="inptnm"/><input type="submit" name="button" id="button" value="&nbsp;" class="buttondl" /></form>
                               <div>发表:(2~500个字数以内)&nbsp;&nbsp;<a href="http://bbs.52holiday.com/register.php" target="_blank" title="点这里去注册帐号">还不是会员？赶紧来注册吧！</a> </div>';
		}
		break;

	case 'islogin':
		include(SITE_ROOT.'./uc_client/client.php');
		list($uid, $username) = empty($_COOKIE[$cookiepre.'cultureusers']) ? array(0, '') : explode("\t", uc_authcode($_COOKIE[$cookiepre.'cultureusers'], 'DECODE'));
		if($uid > 0)
		{
			if($mod == 'index')
			{
				$data = "<div class=\"login_after\">
						<div><a href=\"http://bbs.52holiday.com/space-uid-{$uid}.html\" target=\"_blank\"><img src=\"http://ucenter.52holiday.com/avatar.php?uid={$uid}&size=small\" /></a></div>
						<div><a href=\"http://bbs.52holiday.com/space-uid-{$uid}.html\" target=\"_blank\" style=\"font-size:14px; font-weight:bold;\">{$username}</a> <span style=\"color:red; font-size:14px; font-weight:bold;\">欢迎您回来！</span></div>
						<div><a href=\"http://bbs.52holiday.com/pm.php\" target=\"_blank\">短消息</a> <a href=\"http://bbs.52holiday.com/my.php?item=threads\" target=\"_blank\" style=\"color:red;\">我的帖子</a> <a href=\"http://bbs.52holiday.com/memcp.php\" target=\"_blank\">个人中心</a></div>
						<div><a href=\"javascript:logout('index');\">退出登陆</a></div>
					</div>";
			}
			else
			{
				$data =  "欢迎您,<a id=\"spaceid\" href=\"http://home.52holiday.com/space.php?uid=".$uid."\" target=\"_blank\">".$username."</a> [<a href=\"javascript:;\" onclick=\"logout();\">退出</a>]";
			}
		}
		break;

	default:
		return ;
		break;
}
exit($data);
?>